package com.demo2do.fms.admin.web.controller.supplier.reform;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Preloading;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.search.supplier.ReformSearch;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.supplier.ReformService;
import com.demo2do.fms.admin.service.supplier.SupplierService;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.entity.reform.Reform;
import com.demo2do.fms.entity.supplier.common.Supplier;

/**
 * 供方约谈整改
 *
 * @author Ark
 *
 */
@Controller
@RequestMapping("/reforms")
@MenuSetting("menu-supplier")
public class ReformController {
    
    private static final Logger logger = LoggerFactory.getLogger(ReformController.class);

    @Autowired
    private ReformService reformService;
    
    @Autowired
    private SupplierService supplierService;
    
    /**
     * list 
     * 
     * @param login
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-supplier-reform-originate-management")
    public ModelAndView list(@Secure Login login, HttpServletRequest request, Page page) {
        
        ModelAndView modelAndView = new ModelAndView("supplier/reform/management/reform-list");
        
        int totals = reformService.count(login);
        if (totals > 0) {
            
            List<Reform> reforms = reformService.list(login, page.with(totals));
            modelAndView.addObject("reforms", reforms);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * search panel
     * 
     * @param login
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public ModelAndView search(@Secure Login login) {
        
        ModelAndView modelAndView = new ModelAndView("supplier/reform/management/reform-search");
        
        List<Supplier> suppliers = supplierService.listSubSupplier(login);
        modelAndView.addObject("suppliers", suppliers);
        
        return modelAndView;
        
    }
    
    /**
     * search
     * 
     * @param search
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-supplier-reform-originate-management")
    public ModelAndView onSearch(ReformSearch search, @Secure Login login, 
                                 HttpServletRequest request, Page page) {
        
        ModelAndView modelAndView = new ModelAndView("supplier/reform/management/reform-list");
        
        if (login.isSupplier()) {
            search.setOriginateId(login.getSupplierId());
        }
        
        int totals = reformService.count(search);
        if (totals > 0) {
            
            List<Reform> reforms = reformService.search(search, page.with(totals));
            modelAndView.addObject("reforms", reforms);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "filter", "page"));
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * view
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @MenuSetting("submenu-supplier-reform-originate-management")
    public ModelAndView view(@PathVariable("id") Long id) {
        
        ModelAndView modelAndView = new ModelAndView("supplier/reform/management/reform-view");
        
        Reform reform = reformService.load(Reform.class, id);
        
        modelAndView.addObject("reform", reform);
        
        return modelAndView;
        
    }
    
    /**
     * create panel
     * 
     * @param login
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public ModelAndView create(@Secure Login login) {
        
        ModelAndView modelAndView = new ModelAndView("supplier/reform/management/reform-create");
        
        List<Supplier> suppliers = supplierService.listSubSupplier(login);
        modelAndView.addObject("suppliers", suppliers);
        
        return modelAndView;
        
    }
    
    /**
     * create
     * 
     * @param reform
     * @param operator
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onCreate(Reform reform, @Secure Login login) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            reformService.create(reform, login);
            
            httpResult.success().message("创建成功");
            
        } catch (Exception e) {
            httpResult.fail("500").message("创建失败! " + e.getMessage());
            logger.error("#onCreate() - create failed, exception=[{}]", e);
        }
        
        return httpResult;
        
    }
    
    /**
     * edit panel
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable("id") Long id) {
        
        ModelAndView modelAndView = new ModelAndView("supplier/reform/management/reform-edit");
        
        Reform reform = reformService.load(Reform.class, id);
        
        modelAndView.addObject("reform", reform);
        
        return modelAndView;
        
    }
    
    /**
     * edit
     * 
     * @param reform
     * @param operator
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onEdit(@Preloading Reform reform, @Secure(property = "id") Long operator) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            reformService.update(reform.edit(operator));
            
            httpResult.success().message("修改成功");
            
        } catch (Exception e) {
            httpResult.fail("500").message("修改失败! " + e.getMessage());
            logger.error("#onEdit() - edit failed, exception=[{}]", e);
        }
        
        return httpResult;
        
    }
    
    /**
     * close panel
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/close", method = RequestMethod.GET)
    public ModelAndView close(@PathVariable("id") Long id) {
        
        ModelAndView modelAndView = new ModelAndView("supplier/reform/management/reform-close");
        
        modelAndView.addObject("id", id);
        
        return modelAndView;
        
    }
    
    /**
     * close
     * 
     * @param reform
     * @param operator
     * @return
     */
    @RequestMapping(value = "/{id}/close", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onClose(@PathVariable("id") Long id, @Secure(property = "id") Long operator) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            Reform reform = reformService.load(Reform.class, id);
            reformService.update(reform.close(operator));
            
            httpResult.success().message("关闭成功");
            
        } catch (Exception e) {
            httpResult.fail("500").message("关闭失败! " + e.getMessage());
            logger.error("#onClose() - close failed, exception=[{}]", e);
        }
        
        return httpResult;
        
    }
    
    /**
     * @param id
     * @param type
     * @return
     */
    @RequestMapping(value = "/{id}/upload-annex", method = RequestMethod.GET)
    public ModelAndView uploadAnnex(@PathVariable("id") Long id,
                                    @RequestParam("type") String type){
        
        ModelAndView modelAndView = new ModelAndView("supplier/reform/management/reform-annex-upload");
        
        modelAndView.addObject("reformId", id);
        modelAndView.addObject("type", type);
        
        return modelAndView;
        
    }
    
}
